package com.example.wechatapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.wechatapp.adapter.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ViewPager2 viewPager2;
    private LinearLayout llChat,llPeople,llFind,llMe;
    private ImageView ivWx,ivPeople,ivFind,ivMe,imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPage();
        initTableAndView();
    }
    private void initTableAndView() {
        llChat=findViewById(R.id.table_wx);
        llChat.setOnClickListener(this);
        llPeople=findViewById(R.id.table_people);
        llPeople.setOnClickListener(this);
        llFind=findViewById(R.id.table_find);
        llFind.setOnClickListener(this);
        llMe=findViewById(R.id.table_me);
        llMe.setOnClickListener(this);

        ivWx=findViewById(R.id.tab_iv_wx);
        ivPeople=findViewById(R.id.tab_iv_people);
        ivFind=findViewById(R.id.tab_iv_find);
        ivMe=findViewById(R.id.tab_iv_me);

        ivWx.setSelected(true);
        imageView=ivWx;

    }

    private void initPage() {
        viewPager2=findViewById(R.id.vp2);
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(BlankFragment.newInstance("微信聊天"));
        fragments.add(BlankFragment.newInstance("通讯录"));
        fragments.add(BlankFragment.newInstance("发现"));
        fragments.add(BlankFragment.newInstance("我"));
        MyAdapter myAdapter=new MyAdapter(getSupportFragmentManager(), getLifecycle(),fragments);
        viewPager2.setAdapter(myAdapter);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTable(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

    private void changeTable(int position) {
        imageView.setSelected(false);
        switch (position){
            case R.id.table_wx:
                viewPager2.setCurrentItem(0);
            case 0:
                ivWx.setSelected(true);
                imageView=ivWx;
                break;
            case R.id.table_people:
                viewPager2.setCurrentItem(1);
            case 1:
                ivPeople.setSelected(true);
                imageView=ivPeople;
                break;
            case R.id.table_find:
                viewPager2.setCurrentItem(2);
            case 2:
                ivFind.setSelected(true);
                imageView=ivFind;
                break;
            case R.id.table_me:
                viewPager2.setCurrentItem(3);
            case 3:
                ivMe.setSelected(true);
                imageView=ivMe;
                break;
        }
    }

    @Override
    public void onClick(View v) {
        changeTable(v.getId());
    }
}